﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AliCharacter_recognition.Utils.BaiduTool
{
    internal class BaiduOcrhelper
    {
        /// <summary>
        /// API_KEY保存路径
        /// </summary>
        public readonly static string API_KEY_Path = StaticValue.BinPath + "OcrAPIKEY.txt";

        /// <summary>
        /// SECRET_KEY保存路径
        /// </summary>
        public readonly static string SECRET_KEY_Path = StaticValue.BinPath + "OcrSECRETKEY.txt";

        public static string ReadFile(string FilePath)
        {
            try
            {
                
                Baidu.Aip.Ocr.Ocr client = CreateClient();
                var image = File.ReadAllBytes(FilePath);
                // 调用通用文字识别, 图片参数为本地图片，可能会抛出网络等异常，请使用try/catch捕获
                var result = client.AccurateBasic(image);
                Console.WriteLine(result);
                return result.ToString();
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        /// <summary>
        /// 处理阅读身份证
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="idCardSide">标记正反面， back背面 front正面</param>
        /// <returns></returns>
        public static string ReadIDCardFile(string FilePath, string idCardSide)
        {
            try
            {
                Baidu.Aip.Ocr.Ocr client = CreateClient();
                var image = File.ReadAllBytes(FilePath);
                // 调用通用文字识别, 图片参数为本地图片，可能会抛出网络等异常，请使用try/catch捕获

                // 调用身份证识别，可能会抛出网络等异常，请使用try/catch捕获
                var result = client.Idcard(image, idCardSide);
                Console.WriteLine(result);
                return result.ToString();
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        /// <summary>
        /// 创建主服务
        /// </summary>
        /// <returns></returns>
        private static Baidu.Aip.Ocr.Ocr CreateClient()
        {
            var client = new Baidu.Aip.Ocr.Ocr(FileHelper.ReadAllText(API_KEY_Path), FileHelper.ReadAllText(SECRET_KEY_Path));
            client.Timeout = 60000;  // 修改超时时间
            return client;
        }

        /// <summary>
        ///处理阅读营业执照
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string ReadBusinessLicense(string FilePath)
        {
            try
            {
                Baidu.Aip.Ocr.Ocr client = CreateClient();
                var image = File.ReadAllBytes(FilePath);
                // 调用营业执照识别，可能会抛出网络等异常，请使用try/catch捕获
                var result = client.BusinessLicense(image);
                Console.WriteLine(result);
                return result.ToString();
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        /// <summary>
        /// 读取通用票据
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string ReadReceipt(string FilePath)
        {
            try
            {
                Baidu.Aip.Ocr.Ocr client = CreateClient();
                var image = File.ReadAllBytes(FilePath);
                // 调用通用票据识别，可能会抛出网络等异常，请使用try/catch捕获

                // 如果有可选参数
                var options = new Dictionary<string, object>{
        {"recognize_granularity", "big"},
        {"probability", "true"},
        {"accuracy", "normal"},
        {"detect_direction", "true"}
    };
                // 带参数调用通用票据识别
                var result = client.Receipt(image, options);
                Console.WriteLine(result);
                return result.ToString();
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }
            return "";
        }

        /// <summary>
        ///处理阅读银行卡
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string ReadBankCard(string FilePath)
        {
            try
            {
                Baidu.Aip.Ocr.Ocr client = CreateClient();
                var image = File.ReadAllBytes(FilePath);
                // 调用营业执照识别，可能会抛出网络等异常，请使用try/catch捕获
                var result = client.Bankcard(image);
                Console.WriteLine(result);
                FileHelper.WriteText("txet.txt", result.ToString());
                return result.ToString();
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }
    }
}
